<%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true"
    CodeFile="08_Master_Details_AddDelEdit.aspx.cs" Inherits="_08_Master_Details_AddDelEdit"
    Title="Untitled Page" %>

<%@ Register Assembly="Flan.Controls" Namespace="Flan.Controls" TagPrefix="cc1" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
    <asp:UpdateProgress ID="UpdateProgress1" runat="server" AssociatedUpdatePanelID="UpdatePanel1"
        DisplayAfter="0" DynamicLayout="False">
        <progresstemplate>
            <div style="position: absolute; background-color: White; padding: 2px">
                <img src="../Resources/Images/indicator.gif" />
                Loading...
            </div>
        </progresstemplate>
    </asp:UpdateProgress>
    <asp:UpdateProgress ID="UpdateProgress2" runat="server" AssociatedUpdatePanelID="UpdatePanel2"
        DisplayAfter="0" DynamicLayout="False">
        <progresstemplate>
            <div style="position: absolute; background-color: White; padding: 2px">
                <img src="../Resources/Images/indicator.gif" />
                Loading...
            </div>
        </progresstemplate>
    </asp:UpdateProgress>
    <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="False">
        <contenttemplate>
            <div style="font-size: smaller">
                To add a new record, click on the icon at the top left of the table below.</div>
            <asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1" OnRowCommand="GridView1_RowCommand"
                Font-Names="Verdana" AllowPaging="True" ForeColor="#333333" CellPadding="4" AutoGenerateColumns="False"
                DataKeyNames="SupplierID">
                <FooterStyle BackColor="#5D7B9D" ForeColor="White" Font-Bold="True"></FooterStyle>
                <Columns>
                    <asp:TemplateField ShowHeader="False">
                        <HeaderTemplate>
                            <asp:ImageButton ID="ImageButton1" runat="server" ToolTip="Show Details" ImageUrl="~/Resources/Images/newrecord_icon.gif"
                                CommandName="AddRecord" CausesValidation="False"></asp:ImageButton>
                        </HeaderTemplate>
                        <ItemTemplate>
                            <asp:ImageButton ID="ImageButton1" runat="server" ToolTip="Show Details" ImageUrl="~/Resources/Images/details_icon.gif"
                                CommandName="Select" CausesValidation="False"></asp:ImageButton>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:BoundField DataField="CompanyName" SortExpression="CompanyName" HeaderText="CompanyName">
                    </asp:BoundField>
                    <asp:BoundField DataField="ContactName" SortExpression="ContactName" HeaderText="ContactName">
                    </asp:BoundField>
                    <asp:BoundField DataField="ContactTitle" SortExpression="ContactTitle" HeaderText="ContactTitle">
                    </asp:BoundField>
                    <asp:BoundField DataField="Country" SortExpression="Country" HeaderText="Country"></asp:BoundField>
                    <asp:BoundField DataField="City" SortExpression="City" HeaderText="City"></asp:BoundField>
                </Columns>
                <RowStyle BackColor="#F7F6F3" ForeColor="#333333"></RowStyle>
                <EditRowStyle BackColor="#999999"></EditRowStyle>
                <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Left"></PagerStyle>
                <HeaderStyle BackColor="#5D7B9D" ForeColor="White" Font-Bold="True"></HeaderStyle>
                <AlternatingRowStyle BackColor="White" ForeColor="#284775"></AlternatingRowStyle>
            </asp:GridView>
            <asp:SqlDataSource ID="SqlDataSource1" runat="server" SelectCommand="SELECT [SupplierID], [CompanyName], [ContactName], [ContactTitle], [Country], [City] FROM [Suppliers]"
                ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"></asp:SqlDataSource>
        </contenttemplate>
    </asp:UpdatePanel>
    <!--------------UpdatePanelPopupExtender-------------------------->
    <cc1:UpdatePanelPopupExtender ID="UpdatePanelPopupExtender2" runat="server" TargetControlID="UpdatePanel2"
        OffsetX="0" OffsetY="0" CalloutColor="WhiteSmoke" CalloutBorderColor="Gray" />
    <!--------------UpdatePanelPopupExtender-------------------------->
    <div style="display: none">
        <asp:UpdatePanel ID="UpdatePanel2" runat="server" UpdateMode="Conditional">
            <contenttemplate>
<DIV id="divClose" class="closeIcon" runat="server"></DIV><asp:DetailsView id="DetailsView1" runat="server" DataSourceID="SqlDataSource2" CellPadding="4" ForeColor="#333333" OnItemCommand="DetailsView1_ItemCommand" BorderWidth="1px" BorderStyle="Solid" BorderColor="Gray" OnItemUpdated="DetailsView1_ItemUpdated" AutoGenerateRows="False" AutoGenerateEditButton="True" HeaderText="Details" OnItemDeleted="DetailsView1_ItemDeleted" OnItemInserted="DetailsView1_ItemInserted" OnDataBound="DetailsView1_DataBound">
<FooterStyle BackColor="#1C5E55" ForeColor="White" Font-Bold="True"></FooterStyle>

<CommandRowStyle BackColor="LemonChiffon" HorizontalAlign="Left" Font-Bold="True"></CommandRowStyle>

<EditRowStyle BorderStyle="None"></EditRowStyle>

<RowStyle BackColor="#E3EAEB"></RowStyle>

<PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center"></PagerStyle>
<Fields>
<asp:TemplateField SortExpression="CompanyName" HeaderText="CompanyName"><EditItemTemplate>
                                <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("CompanyName") %>'></asp:TextBox>
                                <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="Required!"
                                    Display="Dynamic" ControlToValidate="TextBox1"></asp:RequiredFieldValidator>
                            
</EditItemTemplate>
<InsertItemTemplate>
                                <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("CompanyName") %>'></asp:TextBox>
                                <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="Required!"
                                    Display="Dynamic" ControlToValidate="TextBox1"></asp:RequiredFieldValidator>&nbsp;
                            
</InsertItemTemplate>
<ItemTemplate>
<asp:Label id="Label1" runat="server" Text='<%# Bind("CompanyName") %>'></asp:Label> 
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="ContactName" SortExpression="ContactName" HeaderText="ContactName"></asp:BoundField>
<asp:BoundField DataField="ContactTitle" SortExpression="ContactTitle" HeaderText="ContactTitle"></asp:BoundField>
<asp:BoundField DataField="Address" SortExpression="Address" HeaderText="Address"></asp:BoundField>
<asp:BoundField DataField="City" SortExpression="City" HeaderText="City"></asp:BoundField>
<asp:BoundField DataField="Region" SortExpression="Region" HeaderText="Region"></asp:BoundField>
<asp:BoundField DataField="PostalCode" SortExpression="PostalCode" HeaderText="PostalCode"></asp:BoundField>
<asp:BoundField DataField="Country" SortExpression="Country" HeaderText="Country"></asp:BoundField>
<asp:BoundField DataField="Phone" SortExpression="Phone" HeaderText="Phone"></asp:BoundField>
<asp:BoundField DataField="Fax" SortExpression="Fax" HeaderText="Fax"></asp:BoundField>
<asp:CommandField ShowDeleteButton="True"></asp:CommandField>
</Fields>

<FieldHeaderStyle BackColor="#D0D0D0" Font-Bold="True"></FieldHeaderStyle>

<HeaderStyle BackColor="DimGray" ForeColor="White" Font-Bold="True"></HeaderStyle>

<AlternatingRowStyle BackColor="White"></AlternatingRowStyle>
</asp:DetailsView> <asp:SqlDataSource id="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>" SelectCommand="SELECT [SupplierID], [CompanyName], [ContactName], [ContactTitle], [Address], [City], [Region], [PostalCode], [Country], [Phone], [Fax] FROM [Suppliers] WHERE ([SupplierID] = @SupplierID)" UpdateCommand="UPDATE [Suppliers] SET [CompanyName] = @CompanyName, [ContactName] = @ContactName, [ContactTitle] = @ContactTitle, [Address] = @Address, [City] = @City, [Region] = @Region, [PostalCode] = @PostalCode, [Country] = @Country, [Phone] = @Phone, [Fax] = @Fax WHERE [SupplierID] = @SupplierID" DeleteCommand="DELETE FROM [Suppliers] WHERE [SupplierID] = @SupplierID" InsertCommand="INSERT INTO [Suppliers] ([CompanyName], [ContactName], [ContactTitle], [Address], [City], [Region], [PostalCode], [Country], [Phone], [Fax]) VALUES (@CompanyName, @ContactName, @ContactTitle, @Address, @City, @Region, @PostalCode, @Country, @Phone, @Fax)">
                    <DeleteParameters>
                        <asp:ControlParameter Type="Int32" Name="SupplierID" PropertyName="SelectedValue"
                            ControlID="GridView1"></asp:ControlParameter>
                    </DeleteParameters>
                    <UpdateParameters>
                        <asp:Parameter Type="String" Name="CompanyName"></asp:Parameter>
                        <asp:Parameter Type="String" Name="ContactName"></asp:Parameter>
                        <asp:Parameter Type="String" Name="ContactTitle"></asp:Parameter>
                        <asp:Parameter Type="String" Name="Address"></asp:Parameter>
                        <asp:Parameter Type="String" Name="City"></asp:Parameter>
                        <asp:Parameter Type="String" Name="Region"></asp:Parameter>
                        <asp:Parameter Type="String" Name="PostalCode"></asp:Parameter>
                        <asp:Parameter Type="String" Name="Country"></asp:Parameter>
                        <asp:Parameter Type="String" Name="Phone"></asp:Parameter>
                        <asp:Parameter Type="String" Name="Fax"></asp:Parameter>
                        <asp:ControlParameter Type="Int32" Name="SupplierID" PropertyName="SelectedValue"
                            ControlID="GridView1"></asp:ControlParameter>
                    </UpdateParameters>
                    <SelectParameters>
                        <asp:ControlParameter Type="Int32" Name="SupplierID" PropertyName="SelectedValue"
                            ControlID="GridView1"></asp:ControlParameter>
                    </SelectParameters>
                    <InsertParameters>
                        <asp:Parameter Type="String" Name="CompanyName"></asp:Parameter>
                        <asp:Parameter Type="String" Name="ContactName"></asp:Parameter>
                        <asp:Parameter Type="String" Name="ContactTitle"></asp:Parameter>
                        <asp:Parameter Type="String" Name="Address"></asp:Parameter>
                        <asp:Parameter Type="String" Name="City"></asp:Parameter>
                        <asp:Parameter Type="String" Name="Region"></asp:Parameter>
                        <asp:Parameter Type="String" Name="PostalCode"></asp:Parameter>
                        <asp:Parameter Type="String" Name="Country"></asp:Parameter>
                        <asp:Parameter Type="String" Name="Phone"></asp:Parameter>
                        <asp:Parameter Type="String" Name="Fax"></asp:Parameter>
                    </InsertParameters>
                </asp:SqlDataSource> 
</contenttemplate>
            <triggers>
                <asp:AsyncPostBackTrigger ControlID="GridView1" EventName="SelectedIndexChanged"></asp:AsyncPostBackTrigger>
            </triggers>
        </asp:UpdatePanel>
        &nbsp;&nbsp;
    </div>
</asp:Content>
<asp:Content ID="Content2" runat="server" ContentPlaceHolderID="ContentPlaceHolder2">
    <ul>
        <li>The "Details icon" in GridView1 is an ImageButton with CommandName="Select". The
            top left icon is an ImageButton with CommandName = "AddRecord".</li><li>UpdatePanel1
                has its ChildrenAsTriggers="False" and UpdateMode is "conditional". We don't
                need to refresh UpdatePanel1 when the "Select" command fires because nothing visually
                changes in UpdatePanel1.</li><li>UpdatePanel2 is set to refresh anytime the SelectedIndex
                    of GridView1 changes</li><li>See codebehind comments for more details.</li></ul>
</asp:Content>
